package com.jerei.web.utils.ui;

import com.alibaba.fastjson.JSONObject;
import com.jerei.utils.UtilsCommon;
import com.jerei.web.utils.UtilsWeb;
import freemarker.core.Environment;
import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.BeansWrapperBuilder;
import freemarker.ext.beans.SimpleMapModel;
import freemarker.template.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.Writer;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by sunfeihu on 2016/11/5.
 */
@Component
public class ButtonForEditTag implements TemplateDirectiveModel {

    @Autowired
    private HttpServletRequest request;

    private LinkedHashMap<String, JSONObject> getBtnMap() {
        LinkedHashMap<String, JSONObject> btnMap = new LinkedHashMap<String, JSONObject>();
        JSONObject obj = new JSONObject();
        obj.put("name", "关闭");
        obj.put("icon", "no");
        obj.put("inAll", true);
        btnMap.put("close", obj);

        Long id = UtilsWeb.getLong(request, "id");
        //如果有主键，则是保存修改
        if (null != id && id > 0) {
            obj = new JSONObject();
            obj.put("name", "保存并修改");
            obj.put("icon", "save");
            obj.put("inAll", true);
            btnMap.put("submit-update", obj);
        } else {//如果没有主键，怎是保存新建
            obj = new JSONObject();
            obj.put("name", "保存并新建");
            obj.put("icon", "save");
            obj.put("inAll", true);
            btnMap.put("submit-new", obj);
        }

        obj = new JSONObject();
        obj.put("name", "保存并关闭");
        obj.put("icon", "save");
        obj.put("inAll", true);
        btnMap.put("submit-close", obj);

        obj = new JSONObject();
        obj.put("name", "打印");
        obj.put("icon", "print");
        obj.put("inAll", true);
        btnMap.put("print", obj);

        obj = new JSONObject();
        obj.put("name", "导出");
        obj.put("icon", "export");
        obj.put("inAll", true);
        btnMap.put("export", obj);

        obj = new JSONObject();
        obj.put("name", "选择");
        obj.put("icon", "ok");
        obj.put("inAll", false);
        btnMap.put("choose", obj);

        return btnMap;
    }

    @Override
    public void execute(Environment env, Map params, TemplateModel[] models, TemplateDirectiveBody body) throws TemplateException, IOException {
        String btns = UtilsCommon.getString(params.get("btns"));
        LinkedHashMap<String, JSONObject> btnMap = this.getBtnMap();
        BeansWrapper beansWrapper = new BeansWrapperBuilder(Configuration.getVersion()).build();
        LinkedHashMap<String, JSONObject> canUseBtnMap = new LinkedHashMap<String, JSONObject>();
        //只要不是全部，进行按钮筛选
        if (!"all".equals(btns)) {
            String[] btnArr = btns.split(",");
            JSONObject btn = null;
            for (int i = 0; i < btnArr.length; i++) {
                btn = btnMap.get(btnArr[i]);
                if (null != btn) {
                    canUseBtnMap.put(btnArr[i], btn);
                }
            }
        } else {//如果是全部按钮
            JSONObject btn = null;
            for (String btnKey : btnMap.keySet()) {
                btn = btnMap.get(btnKey);
                if (null != btn && btn.getBoolean("inAll")) {
                    canUseBtnMap.put(btnKey, btn);
                }
            }
        }
        env.setLocalVariable("btnMap", new SimpleMapModel(canUseBtnMap, beansWrapper));
        Writer out = env.getOut();
        if (body != null) {
            body.render(env.getOut());
        } else {
            throw new RuntimeException("标签内部至少要加一个空格");
        }
    }
}
